package cn.itcast.dao.impl;

import cn.itcast.dao.ChangeListDao;
import cn.itcast.domain.ChangeList;
import cn.itcast.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ChangeListDaoImpl  implements ChangeListDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public void updateChangeListInfo(int parseInt, int parseInt1, String info) {
        String sql="update changelist set info_a=?,info_b=?,state_id=?,change_state=?,check_state=? where id=? and yid=?";
        try {
            template.update(sql,info,"empty",1,0,0,parseInt,parseInt1);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateChangeInfob(String info_b, int id) {
        String sql="update changelist set info_b=? where id=? ";
        try {
            template.update(sql,info_b,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ChangeList> findAll() {
        //1定义sql
        String sql="select *from changelist";
        List<ChangeList>  changelists=null;
        try {
            changelists = template.query(sql, new BeanPropertyRowMapper<ChangeList>(ChangeList.class));
            return  changelists;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public ChangeList findChangeListById(int parseInt) {
        String sql="select*from changelist where id = ?";
        ChangeList changelist=null;
        try {
            changelist=template.queryForObject(sql,new BeanPropertyRowMapper<ChangeList>(ChangeList.class),parseInt);
            return changelist;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public void updateChangeListStateId(ChangeList changelist) {
        String sql="update changelist set state_id=? ,change_state=0,check_state=0 where id=?";
        try {
            template.update(sql,changelist.getState_id(),changelist.getId());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ChangeList> findAllByStateId() {
        String sql="select *from changelist where state_id=2";
        List<ChangeList> changelists=null;
        try {
            changelists = template.query(sql, new BeanPropertyRowMapper<ChangeList>(ChangeList.class));
            return changelists;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public ChangeList findChangeListByIda(int parseInt) {
        String sql="select * from changelist where id=?";
        ChangeList changelist=null;
        try {
            changelist=template.queryForObject(sql,new BeanPropertyRowMapper<ChangeList>(ChangeList.class),parseInt);
            return changelist;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public void updateChangeListChangeState(ChangeList changelist) {
        String sql="update changelist set change_state=? ,check_state=0 where id=?";
        try {
            template.update(sql,changelist.getChange_state(),changelist.getId());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ChangeList> findAllByChangeState() {
        String sql="select *from changelist where change_state=1";
        List<ChangeList> changelists=null;
        try {
            changelists = template.query(sql, new BeanPropertyRowMapper<ChangeList>(ChangeList.class));
            return changelists;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void updateChangeListCheckState(ChangeList changelist) {
        String sql="update changelist set check_state=? where id = ?";
        template.update(sql,changelist.getCheck_state(),changelist.getId());
    }

    @Override
    public void addChangeList(int id, String info_a, int yid) {
        String sql="insert into changelist values(?,?,?,'empty',1,?,0,0)";
        try {
            template.update(sql,id,id,info_a,yid);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }
}
